package com.ycfmedical.bed.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.listener.Receivedata;

import cn.droidlover.xdroidmvp.log.XLog;

/**
 * 生命体征历史界面弹出日历Dialog
 * Created by Administrator on 2017-04-10.
 */
@SuppressLint("ValidFragment")
public class VsSelectTimeDialog extends DialogFragment implements OnDateSelectedListener, View.OnClickListener {
    private Context context;
    private MaterialCalendarView materialCalendarView;
    private int day = 2017321;
    private View view;
    private TextView confirm, cancel;
    private Receivedata receivedata;
    private String selectDay = null;
    private String Time;

    public VsSelectTimeDialog(Receivedata receivedata){
        this.receivedata = receivedata;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = inflater.inflate(R.layout.dialog_fragment, container);
        initView();
        click();
        return view;
    }

    private void click() {
        materialCalendarView.setOnDateChangedListener(this);
        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    private void initView() {
        confirm = (TextView) view.findViewById(R.id.confirm_select);
        cancel = (TextView) view.findViewById(R.id.cancel_select);
        materialCalendarView = (MaterialCalendarView) view.findViewById(R.id.calendarView);
        materialCalendarView.setSelected(true);
        context = getContext();
        setCancelable(false);
    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
        String Month = ""+(date.getMonth()+1);
        String day = ""+date.getDay();

        if(Month.length()<2){
            Month = "0"+Month;
        }
        if(day.length()<2){
            day = "0"+day;
        }
        Time = date.getYear()+"-"+Month+"-"+day;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm_select:
                if(Time != null){
                    receivedata.Receive(Time, Constants.NEWID);
                    dismiss();
                }
                break;
            case R.id.cancel_select:
                dismiss();
                break;
            default:
                break;
        }
    }
}
